/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VistaProducto.java
 *
 * Created on 06/04/2011, 02:12:56 AM
 */

package vistas;

import controladores.ControladorProducto;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import modelos.ModeloTabla;

/**
 *
 * @author usuario
 */
public class VistaProducto extends javax.swing.JFrame {

    private Vector<JTextField> textos;
    private Vector<JButton> botones;
    private ControladorProducto control;
    private ModeloTabla modeloTabla;
    private Vector<String> identificadores, fila;

    /** Creates new form VistaProducto */
    public VistaProducto() {
        initComponents();
        modeloTabla= new ModeloTabla();
        tblIngrediente.setModel(modeloTabla);

        identificadores= new Vector<String>();

        identificadores.add("Ingrediente");
        identificadores.add("Porcion");
        modeloTabla.setColumnIdentifiers(identificadores);
        
        botones = new Vector<JButton>();
        botones.add(btnSalir);
        botones.add(btnConsultar);
        botones.add(btnGenerar);
        botones.add(btnRegistrar);
        botones.add(btnCancelar);
        botones.add(btnEliminar);
        botones.add(btnMas);
        botones.add(btnMenos);
        textos = new Vector<JTextField>();

        textos.add(txtCodigo);
        textos.add(txtDescripcion);
        textos.add(txtPorcion);
        textos.add(txtPrecio);

        control= new ControladorProducto(this);
        btnCancelar.setActionCommand("Cancelar");
        btnConsultar.setActionCommand("Consultar");
        btnRegistrar.setActionCommand("Registrar");
        btnSalir.setActionCommand("Salir");
        btnGenerar.setActionCommand("Generar");
        btnEliminar.setActionCommand("Eliminar");
        btnMas.setActionCommand("Mas");
        btnMenos.setActionCommand("Menos");
        for (JButton boton : botones) {
            boton.addActionListener(control);
        }
        cancelar();
    }

    public boolean validarVacios(){
        for (JTextField texto : textos)
            if(texto.getText().isEmpty())
                return false;
        return true;
    }

    public boolean validarNumeros(){
        for (int i = 2; i < textos.size(); i++)
            try{
                 if(Double.parseDouble(textos.elementAt(i).getText())<=0)
                    return false;
            }catch(NumberFormatException ex){
                return false;
            }

        return true;
    }

    public void bloquear(){
        txtCodigo.setEditable(false);
        txtPorcion.setEditable(false);
        btnMas.setEnabled(false);
        cmbIngredientes.setEnabled(false);
        btnMenos.setEnabled(false);
    }

    public boolean actualizar(){
        for (int i = 0; i < modeloTabla.getRowCount(); i++)
            if(modeloTabla.getValueAt(i, 0).equals(cmbIngredientes.getItemAt(cmbIngredientes.getSelectedIndex()))){
               modeloTabla.setValueAt(txtPorcion.getText(), i, 1);
               return true;
            }
        return false;
    }


    public void agregar(String dato){
        cmbCategoria.setSelectedItem(dato);
    }

    public void agregar(String dato,boolean actu){
        if(actu)
            if(!actualizar()){
                fila= new Vector<String>();
                fila.add(dato.substring(0, dato.indexOf("|")));
                fila.add(dato.substring(dato.indexOf("|")+1,dato.length()));
                modeloTabla.addRow(fila);
            }
            else;
        else{
                fila= new Vector<String>();
                fila.add(dato.substring(0, dato.indexOf("|")));
                fila.add(dato.substring(dato.indexOf("|")+1,dato.length()));
                modeloTabla.addRow(fila);
        }
    }

    public void eliminar(){
        int i = tblIngrediente.getSelectedRow();
        if(i != -1){
            modeloTabla.removeRow(i);
        }
        else
            JOptionPane.showMessageDialog(new JFrame(),"Debe seleccionar la fila que desea eliminar","Burguer",JOptionPane.ERROR_MESSAGE);

    }

     public void cancelar(){
        for (JTextField texto : textos){
            texto.setEditable(true);
            texto.setText("");
        }
        txtCodigo.setEditable(true);
        txtPorcion.setEditable(true);
        btnMas.setEnabled(true);
        btnMenos.setEnabled(true);
        cmbIngredientes.setEnabled(true);
        modeloTabla.setRowCount(0);
     }

    public void cargarComboIngrediente(Vector<String> descripciones){
        cmbIngredientes.removeAllItems();
        for (int i = 0; i < descripciones.size(); i++) {
            cmbIngredientes.addItem(descripciones.elementAt(i));

        }
    }


    public void cargarComboCategoria(Vector<String> descripciones){
        cmbCategoria.removeAllItems();
        for (int i = 0; i < descripciones.size(); i++) {
            cmbCategoria.addItem(descripciones.elementAt(i));

        }
    }

    public JTable getTblIngrediente() {
        return tblIngrediente;
    }

    public JTextField getTxtCodigo() {
        return txtCodigo;
    }

    public JTextField getTxtDescripcion() {
        return txtDescripcion;
    }

    public JTextField getTxtPorcion() {
        return txtPorcion;
    }

    public JTextField getTxtPrecio() {
        return txtPrecio;
    }

    public String getTextoCmbIngrediente() {
        return (String) cmbIngredientes.getItemAt(cmbIngredientes.getSelectedIndex());
    }

    public String getTextoCmbCategoria() {
        return (String) cmbCategoria.getItemAt(cmbCategoria.getSelectedIndex());
    }

     public Vector<String> getInfoTabla(){
         Vector<String> v = new Vector<String>();
        for (int i = 0; i < modeloTabla.getRowCount(); i++)
            v.add(""+modeloTabla.getValueAt(i, 0)+"|"+modeloTabla.getValueAt(i, 1));
        return v;
    }

     

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel6 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        txtDescripcion = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtPrecio = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtPorcion = new javax.swing.JTextField();
        btnGenerar = new javax.swing.JButton();
        btnConsultar = new javax.swing.JButton();
        btnRegistrar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        btnSalir = new javax.swing.JButton();
        cmbIngredientes = new javax.swing.JComboBox();
        btnMas = new javax.swing.JButton();
        btnMenos = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblIngrediente = new javax.swing.JTable();
        cmbCategoria = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Productos");
        getContentPane().setLayout(null);

        jLabel6.setFont(new java.awt.Font("Arial", 0, 15)); // NOI18N
        jLabel6.setText("Codigo:");
        getContentPane().add(jLabel6);
        jLabel6.setBounds(160, 30, 60, 30);

        jLabel1.setFont(new java.awt.Font("Arial", 0, 13)); // NOI18N
        jLabel1.setText("Categoria:");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(160, 70, 140, 32);

        txtCodigo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCodigoActionPerformed(evt);
            }
        });
        getContentPane().add(txtCodigo);
        txtCodigo.setBounds(280, 30, 190, 30);

        txtDescripcion.setFont(new java.awt.Font("Arial", 0, 13)); // NOI18N
        getContentPane().add(txtDescripcion);
        txtDescripcion.setBounds(280, 110, 190, 32);

        jLabel3.setFont(new java.awt.Font("Arial", 0, 13)); // NOI18N
        jLabel3.setText("Precio:");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(160, 160, 224, 32);

        txtPrecio.setFont(new java.awt.Font("Arial", 0, 13));
        getContentPane().add(txtPrecio);
        txtPrecio.setBounds(280, 160, 190, 30);

        jLabel5.setFont(new java.awt.Font("Arial", 0, 13));
        jLabel5.setText("Procion:");
        getContentPane().add(jLabel5);
        jLabel5.setBounds(300, 210, 55, 30);

        txtPorcion.setFont(new java.awt.Font("Arial", 0, 13));
        getContentPane().add(txtPorcion);
        txtPorcion.setBounds(360, 210, 110, 30);

        btnGenerar.setFont(new java.awt.Font("Arial", 1, 15)); // NOI18N
        btnGenerar.setText("Generar");
        getContentPane().add(btnGenerar);
        btnGenerar.setBounds(480, 50, 120, 30);

        btnConsultar.setFont(new java.awt.Font("Arial", 1, 15)); // NOI18N
        btnConsultar.setText("Consultar");
        getContentPane().add(btnConsultar);
        btnConsultar.setBounds(480, 20, 120, 30);

        btnRegistrar.setFont(new java.awt.Font("Arial", 1, 15)); // NOI18N
        btnRegistrar.setText("Registrar");
        btnRegistrar.setPreferredSize(new java.awt.Dimension(80, 30));
        getContentPane().add(btnRegistrar);
        btnRegistrar.setBounds(70, 480, 120, 30);

        btnEliminar.setFont(new java.awt.Font("Arial", 1, 15));
        btnEliminar.setText("Eliminar");
        btnEliminar.setPreferredSize(new java.awt.Dimension(80, 30));
        getContentPane().add(btnEliminar);
        btnEliminar.setBounds(200, 480, 120, 30);

        btnCancelar.setFont(new java.awt.Font("Arial", 1, 15)); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.setPreferredSize(new java.awt.Dimension(80, 30));
        getContentPane().add(btnCancelar);
        btnCancelar.setBounds(330, 480, 120, 30);

        btnSalir.setFont(new java.awt.Font("Arial", 1, 15)); // NOI18N
        btnSalir.setText("Salir");
        getContentPane().add(btnSalir);
        btnSalir.setBounds(460, 480, 120, 30);

        getContentPane().add(cmbIngredientes);
        cmbIngredientes.setBounds(120, 210, 170, 30);

        btnMas.setFont(new java.awt.Font("Arial", 1, 15)); // NOI18N
        btnMas.setText("+");
        getContentPane().add(btnMas);
        btnMas.setBounds(260, 260, 50, 30);

        btnMenos.setFont(new java.awt.Font("Arial", 1, 15)); // NOI18N
        btnMenos.setText("-");
        getContentPane().add(btnMenos);
        btnMenos.setBounds(350, 260, 50, 30);

        tblIngrediente.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblIngrediente);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(100, 300, 440, 160);

        getContentPane().add(cmbCategoria);
        cmbCategoria.setBounds(280, 70, 190, 30);

        jLabel2.setFont(new java.awt.Font("Arial", 0, 13)); // NOI18N
        jLabel2.setText("Descripcion:");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(160, 110, 140, 32);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-662)/2, (screenSize.height-546)/2, 662, 546);
    }// </editor-fold>//GEN-END:initComponents

    private void txtCodigoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCodigoActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_txtCodigoActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VistaProducto().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnConsultar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnGenerar;
    private javax.swing.JButton btnMas;
    private javax.swing.JButton btnMenos;
    private javax.swing.JButton btnRegistrar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JComboBox cmbCategoria;
    private javax.swing.JComboBox cmbIngredientes;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblIngrediente;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtDescripcion;
    private javax.swing.JTextField txtPorcion;
    private javax.swing.JTextField txtPrecio;
    // End of variables declaration//GEN-END:variables

}
